package com.yuven.appframework.network;

import android.net.ParseException;

import com.yuven.appframework.R;
import com.yuven.baselib.network.errorhandlecomponent.RetrofitException;
import com.yuven.baselib.utils.Logger;
import com.yuven.baselib.utils.ResourceUtil;
import com.google.gson.JsonParseException;

import org.json.JSONException;


public class ExceptionHandle {

    private static final int UNAUTHORIZED = 401;
    private static final int FORBIDDEN = 403;
    private static final int NOT_FOUND = 404;
    private static final int REQUEST_TIMEOUT = 408;
    private static final int INTERNAL_SERVER_ERROR = 500;
    private static final int BAD_GATEWAY = 502;
    private static final int SERVICE_UNAVAILABLE = 503;
    private static final int GATEWAY_TIMEOUT = 504;

    public static ResponseThrowable handleException(Throwable e) {
        ResponseThrowable ex;
        if (e instanceof RetrofitException) {
            RetrofitException retrofitException = (RetrofitException) e;
            ex = new ResponseThrowable(e, ERROR.UNKNOWN);

            Logger.i("ExceptionHandle.handleException is RetrofitException Kind = " + retrofitException.getKind());

            if (retrofitException.getKind() == RetrofitException.Kind.NETWORK) {    //IO异常
//                ex.code = ERROR.IO_ERROR;
//                ex.message = "IO_ERROR异常，请稍后重试";
            } else if (retrofitException.getKind() == RetrofitException.Kind.HTTP) {
                ex.code = ERROR.HTTP_ERROR;
                ex.message = "HTTP_ERROR异常，请稍后重试";
            } else if (retrofitException.getKind() == RetrofitException.Kind.UNEXPECTED) {
                ex.code = ERROR.UNEXPECTED_ERROR;
                ex.message = "内部异常，请稍后重试";
            } else if (retrofitException.getKind() == RetrofitException.Kind.NOCONNECTIVITY) {  //无网络
                ex.code = ERROR.NETWORK_ERROR;
                ex.message = ResourceUtil.getString(R.string.common_network_not_available);
            } else if (retrofitException.getKind() == RetrofitException.Kind.TOKEN) {
                //Token异常
                ex.code = ERROR.TOKEN_ERROR;
                ex.message = "TOKEN_ERROR异常，请稍后重试";
            } else {
                ex.code = ERROR.UNKNOWN;
                ex.message = "UNKNOWN异常，请稍后重试";
            }
            return ex;
        } else if (e instanceof JsonParseException
                || e instanceof JSONException
                || e instanceof ParseException) {
            ex = new ResponseThrowable(e, ERROR.PARSE_ERROR);
            ex.message = "数据解析异常，请稍后重试";
            return ex;
        } else if (e instanceof ExceptionHandle.ServerException){
            ex = new ResponseThrowable(e, ((ServerException) e).code);
            ex.message = e.getMessage();
            return ex;
        } else{
            Logger.i("handleException");
            ex = new ResponseThrowable(e, ERROR.UNKNOWN);
            ex.message = "handleException异常，请稍后重试";
            return ex;
        }
    }


    /**
     * 约定异常
     */
    class ERROR {
        /**
         * 未知错误
         */
        public static final String UNKNOWN = "1000";
        /**
         * 解析错误
         */
        public static final String PARSE_ERROR = "1001";
        /**
         * 网络错误
         */
        public static final String NETWORK_ERROR = "1002";
        /**
         * 非200状态
         */
        public static final String HTTP_ERROR = "1003";

        /**
         * 证书出错
         */
        public static final String SSL_ERROR = "1005";

        /**
         * 连接超时
         */
        public static final String TIMEOUT_ERROR = "1006";
        /**
         * 未知主机异常
         */
        public static final String UNKOWN_HOST = "1007";
        /**
         * I/O异常
         */
        public static final String IO_ERROR = "1008";
        /**
         * TOKEN异常
         */
        public static final String TOKEN_ERROR = "1009";

        /**
         * UNEXPECTED_ERROR
         * An internal error occurred while attempting to execute a request
         */
        public static final String UNEXPECTED_ERROR = "1010";

    }

    //框架层报的异常
    public static class ResponseThrowable extends Exception {
        public String code;
        public String message;

        public ResponseThrowable(Throwable throwable, String code) {
            super(throwable);
            this.code = code;
        }

        @Override
        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }
    }

    //API报的异常
    public static class ServerException extends RuntimeException {

        public String code;

        public ServerException(String code, String message) {
            super(message);
            this.code = code;
        }

    }
}